#include <iostream>
using namespace std;

const int p = 1e3 + 5;
int cost[p], value[p];
int dp[p][p];

int main()
{
    int n, m;
    
    cin >> n >> m;
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d%d", &cost[i], &value[i]);
    }
    
    
    for(int i = 1; i <= m; ++i)
    {
        for(int j = 0; j <= n; ++j)
        {
            dp[i][j] = dp[i - 1][j];
            if(j - cost[i] >= 0)
                dp[i][j] = max(dp[i][j], 
                dp[i - 1][j - cost[i]] + value[i]);
        }
    }
    
    cout << dp[m][n];
    return 0;
}
